<?php
namespace App\Service\Admin;

use Illuminate\Cache\RedisTaggedCache;

class DoctorService extends BaseService
{
    //医生

    /**
     * 列表
     * @param array $pages【start，limit】
     * @param array $pas
     */
    public function getList($pages, $pas = [])
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbDoctor)->where('del', 0);
        if (isset($pas['status']) && $pas['status']) {
            $db = $db->where('is_apply', $pas['status']);
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        //$auths = $this->doctor_applys;
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            //用户信息
            $uOne = (array) $this->dbConn($this->dbUser)
                ->where('id', $v['uid'])
                ->first();
            $realname = !empty($uOne)&&$uOne['realname'] ? $uOne['realname'] : '-';;
            $head = '';
            if ($uOne && file_exists($uOne['head'])) {
                $head = $uOne['head'];
            }
            //擅长
            $skillList = $v['skill'] ? explode('||', $v['skill']) : [];
            $skillStr = $skillList ? implode('，', $skillList) : '-';
            //$auth_name = array_key_exists($v['is_apply'], $auths) ? $auths[$v['is_apply']] : '-';
            $auth_name = '';
            if ($v['is_apply'] == 3) {
                $auth_name = '<b style="color: green;">已通过</b>';
            } else if ($v['is_apply'] == 2) {
                $auth_name = '<b style="color: red;">已拒绝</b>';
            } else {
                $auth_name = '<b style="color: orangered;">申请中</b>';
            }
            $arys[] = [
                'id' => $v['id'],
                'uid' => $v['uid'],
                'realname' => $realname,
                'work_type_name' => $this->cateNameById($v['work_type']),
                'work_title_name' => $this->cateNameById($v['work_title']),
                'hospital_name' => $this->hospitalNameById($v['hospital']),
                'depart_name' => $this->cateNameById($v['hospital']),
                'head' => $head,
                'skillstr' => $skillStr,
                'sick_num' => 0, //已帮助患者数量
                'price' => 0, //价格元
                'is_apply' => $v['is_apply'],
                'is_apply_name' => $auth_name,
                'uptime' => $v['uptime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    //医护审核
    public function auth($id, $status)
    {
        $res = (array) $this->dbConn($this->dbDoctor)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        if ($res['is_apply'] == 2) {
            return $this->toback(-2, '已被拒绝');
        }
        if ($res['is_apply'] == 3) {
            return $this->toback(-2, '已通过审核');
        }
        $status = $status==2 ? 3 : 2;
        $up = $this->dbConn($this->dbDoctor)->where('id', $id)
            ->update(['is_apply' => $status, 'applytime' => toDate()]);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /**
     * 代办列表
     * @param int $uid 医生UID
     * @param array $pages【start，limit】
     */
    public function daibanList($uid, $pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbDaiban)
            ->where('del', 0)
            ->where('did', $uid);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('status', 'asc')
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $uOne = (array) $this->dbConn($this->dbUser)
                ->where('id', $v['uid'])
                ->first();
            $uname = empty($uOne) ? '-' : $uOne['uname'];
            $yiyuan = (array) $this->dbConn($this->dbYiyuan)
                ->where('id', $v['yiyuan'])
                ->first();
            $yiyuan_name = empty($yiyuan) ? '-' : $yiyuan['title'];
            $arys[] = [
                'id' => $v['id'],
                'uid' => $v['uid'],
                'uname' => $uname,
                'yytime' => $v['yytime'],
                'yiyuan_name' => $yiyuan_name,
                'remark' => $v['remark'],
                'status' => $v['status'],
                'addtime' => $v['addtime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

//    //代办详情
//    public function daibanOne($id)
//    {
//        $res = (array) $this->dbConn($this->dbDaiban)
//            ->where('del', 0)
//            ->where('id', $id)
//            ->first();
//        if (empty($res)) {
//            return $this->toback(-2, '没有记录');
//        }
//        $uOne = (array) $this->dbConn($this->dbUser)
//            ->where('id', $res['uid'])
//            ->first();
//        $uname = empty($uOne) ? '-' : $uOne['uname'];
//        $yiyuan = (array) $this->dbConn($this->dbYiyuan)
//            ->where('id', $res['yiyuan'])
//            ->first();
//        $yiyuan_name = empty($yiyuan) ? '-' : $yiyuan['title'];
//        $arys = [
//            'id' => $res['id'],
//            'uid' => $res['uid'],
//            'uname' => $uname,
//            'yytime' => $res['yytime'],
//            'yiyuan_name' => $yiyuan_name,
//            'remark' => $res['remark'],
//            'addtime' => $res['addtime'],
//        ];
//        return $this->toback(0, '', $arys);
//    }

    //代办设置已处理
    public function daibanModify($id)
    {
        $res = (array) $this->dbConn($this->dbDaiban)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        if ($res['status'] == 3) {
            return $this->toback(-2, '已设置');
        }
        $up = $this->dbConn($this->dbDaiban)->where('id', $id)
            ->update(['status' => 3, 'uptime' => toDate()]);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /**
     * 医护列表
     * @param int $uid 医生UID
     * @param array $pages【start，limit】
     */
    public function yihuList($uid, $pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbYihu)
            ->where('del', 0)
            ->where('did', $uid);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('status', 'asc')
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $uOne = (array) $this->dbConn($this->dbUser)
                ->where('id', $v['uid'])
                ->first();
            $uname = empty($uOne) ? '-' : $uOne['uname'];
            $arys[] = [
                'id' => $v['id'],
                'uid' => $v['uid'],
                'uname' => $uname,
                'yytime' => $v['yytime'],
                'yydizhi' => $v['yydizhi'],
                'remark' => $v['remark'],
                'addtime' => $v['addtime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 医陪诊列表
     * @param int $uid 医生UID
     * @param array $pages【start，limit】
     */
    public function peizhenList($uid, $pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbPeizhen)
            ->where('del', 0)
            ->where('doc_uid', $uid);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('status', 'asc')
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $yiyuanOne = (array) $this->dbConn($this->dbYiyuan)
                ->where('id', $v['yiyuan'])
                ->first();
            $yiyuan_name = empty($yiyuanOne) ? '-' : $yiyuanOne['title'];
            $arys[] = [
                'id' => $v['id'],
                'pat_name' => $v['pat_name'],
                'pat_phone' => $v['pat_phone'],
                'yytime' => $v['start_time'].'至'.$v['end_time'],
                'yiyuan_name' => $yiyuan_name,
                'delay' => $v['delay'].'小时',
                'remark' => $v['remark'],
                'addtime' => $v['addtime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }
}